#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <b64/cencode.h>
#include <stdlib.h> 
#include <json-c/json_object.h>

extern char *createJsonPost();
extern char *createAuthorization();

extern char *createAuthorization()
{
	char temp[256] = {0,};
	char temp1[256] = {0,};
	base64_encodestate convertRet;
	base64_init_encodestate(&convertRet);
	char *userPassword =  "superman:wxl123456";
	base64_encode_block(userPassword, strlen(userPassword), (char *)temp1, &convertRet);
	//printf("step: %d\n"
	//		"result: %c\n"
	//		"stepcount: %d\n",
	//		convertRet.step,
	//		convertRet.result,
	//		convertRet.stepcount);
	//base64_encode_blockend((char *)temp1, &convertRet);
	//printf("------------------------------------------------------------------------------\n");
	//printf("step: %d\n"
	//		"result: %c\n"
	//		"stepcount: %d\n",
	//		convertRet.step,
	//		convertRet.result,
	//		convertRet.stepcount);
	sprintf((char *)temp, "authorization : Basic %s", temp1);
	char *ret = (char *)malloc(strlen(temp) + 1);
	strcpy(ret, temp);
	return ret;
}

extern char *createJsonPost()
{
	struct json_object * jsonPost = json_object_new_object();
	struct json_object * title = json_object_new_string("2018-06-07 testing v2");
	struct json_object * content = json_object_new_string("this is conent, 测试中文。");
	struct json_object * status = json_object_new_string("publish");
	json_object_object_add(jsonPost, "title", title);
	json_object_object_add(jsonPost, "content", content);
	json_object_object_add(jsonPost, "status", status);
	char * ret =  (char *)json_object_to_json_string(jsonPost);
	char * ret1 = (char *)malloc(strlen(ret) + 1);
	strcpy(ret1, ret);
	return ret1;
}

 
int main(void)
{
	CURL *curl;
	CURLcode res;

	/* In windows, this will init the winsock stuff */ 
	curl_global_init(CURL_GLOBAL_ALL);

	/* get a curl handle */ 
	curl = curl_easy_init();
	if(curl) {
		struct curl_slist *chunk = NULL;
		char *userPass = createAuthorization();
		//char *requst = "POST /wp/v2/posts";
		//printf("%s\n\n", userPass);
		chunk = curl_slist_append(chunk, userPass);
		chunk = curl_slist_append(chunk, 
				"Accept:application/json");
		chunk = curl_slist_append(chunk, "content-Type:application/json");
		chunk = curl_slist_append(chunk, "Accept-Charset:utf-8");
		//curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, requst);
		curl_easy_setopt(curl,  CURLOPT_HTTPHEADER, chunk);
		/* First set the URL that is about to receive our POST. This URL can
		   just as well be a https:// URL if that is what should receive the
		   data. */ 
		curl_easy_setopt(curl, CURLOPT_URL,"http://www.supermew.top/wp-json/wp/v2/posts/?");
		/* Now specify the POST data */ 
		char *jsonPost = createJsonPost();
		//char *jsonPost = "title : 2018-06-08 testing wp/v2"
		//	"content : just for testing.";
		printf("%s\n", jsonPost);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonPost);
		curl_easy_setopt(curl, CURLOPT_HEADERDATA, stdout);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);

		/* Perform the request, res will get the return code */ 
		res = curl_easy_perform(curl);
		/* Check for errors */ 
		if(res != CURLE_OK)
			fprintf(stderr, "curl_easy_perform() failed: %s\n",
					curl_easy_strerror(res));

		/* always cleanup */ 
		curl_easy_cleanup(curl);
	}
	curl_global_cleanup();
	return 0;
}
